﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace FishScore
{
    public partial class frmAddResult : Form
    {
        public FishGameObject game = null;

        public int matchId { get; set; }
        public int areaId { get; set; }
        public int playerId { get; set; }
        public float value { get; set; }
        public int areaPos { get; set; }

        public frmAddResult()
        {
            InitializeComponent();
        }

        private void frmAddResult_Load(object sender, EventArgs e)
        {
            value = -1;
        }

        private static string[] GenerateIndexArray(int start, int end, int step)
        {
            if (step < 0) 
                return new string[] { };    

            var list = new List<string>();

            for (int v = start; v <= end; v += (step + 1))
            {
                list.Add(v.ToString());
            }

            return list.ToArray();
        }

        public void UpdateUI()
        {
            if (game == null) return;

            Player player = game.GetPlayerByID(playerId);
            if (player == null) return;

            lblID.Text = player.ID.ToString();
            lblName.Text = player.Name;

            Match match = game.GetMatchById(matchId);
            lblMatchIndex.Text = "第" + matchId.ToString() + "场";
            lblArea.Text = areaId.ToString() + "区";

            cbAreaIndex.Items.Clear();
            string[] index = GenerateIndexArray(match.StartPos, match.EndPos, match.SeprateNumber);
            cbAreaIndex.Items.AddRange(index);
            cbAreaIndex.SelectedIndex = 0;
        }

        private void txtResult_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                btnOK.PerformClick();
            }

            if (!char.IsDigit(e.KeyChar) && (e.KeyChar != '.') && (e.KeyChar != (char)8))
            {
                e.Handled = true;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtResult.Text.Length == 0) return;
            if (cbAreaIndex.SelectedIndex < 0) return;
            value = float.Parse(txtResult.Text);

            areaPos = Int32.Parse(cbAreaIndex.SelectedItem as string);
        }

        private void txtResult_TextChanged(object sender, EventArgs e)
        {
            btnOK.Enabled = (txtResult.Text.Length != 0);
        }
    }
}